import json
import tornado.ioloop
import tornado.options
from tornado.web import Application, RequestHandler
import pymongo

from publisher import Publisher


class NewTask(RequestHandler):
    def get(self):
        self.render('task.html')

    def post(self):
        id = self.get_body_argument('taskid')
        optype = self.get_body_argument('optype')
        url = self.get_body_argument('url')
        msg = dict(id=id, optype=optype, url=url)
        print('msg is: %s' % msg)
        self.application.publisher.publish_message(msg)


class ShowTask(RequestHandler):
    def get(self):
        """show task result
        """
        pass
        return render('taskresult.html')


class App(Application):
    def __init__(self):
        self.publisher = Publisher()

        handlers = [(r'/newtask', NewTask), (r'/showtask', ShowTask)]

        tornado.web.Application.__init__(self, handlers)

if __name__ == "__main__":
    app = App()
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(1080)
    tornado.ioloop.IOLoop.instance().start()
